# -*- coding: utf-8 -*-


# cmap2owl -- Helper application to convert from concept maps to OWL ontologies
# Copyright (c) 2008-2013  Rodrigo Rizzi Starr
#  
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#  
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#  
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


'''
A phrase corresponds to a concept map phrase and is composed of a first concept
(the left-hand side concept), a linking phrase and the second concept (or the
right hand side concept).

This class is also able to do tagging and tokenization. These can be configured,
but are standardized for portuguese

@author: Rodrigo Rizzi Starr
@copyright: Copyright © 2008-2013 Rodrigo Rizzi Starr
@license: MIT License
@contact: rodrigo.starr@gmail.com
'''


import nltk
from nltk.corpus import mac_morpho


class Phrase(object):
    '''Represents a phrase composed of two concepts connected by a linking
       phrase. It allows several views over this data:
        - A view as a single phrase
        - A view as a single phrase but with contractions expanded, such as 'da'
          to 'de a'
        - After tagging, a view as a tagged phrase
        - Or a view of each component (first concept, linking phrase, second
          concept) also tagged.
    '''

    def __init__(self, lhConcept, linkingPhrase, rhConcept,
                 tokenizer=None):
        self.lhConcept = lhConcept.strip()
        self.rhConcept = rhConcept.strip()
        self.linkingPhrase = linkingPhrase.strip()

        self.lhConceptId = ''
        self.linkingPhraseId = ''
        self.rhConceptId = ''

        if tokenizer == None:
            self.tokenizer = nltk.tokenize.WordPunctTokenizer()

        self.lhConceptTk = self.tokenizer.tokenize(self.lhConcept)
        self.rhConceptTk = self.tokenizer.tokenize(self.rhConcept)
        self.linkingPhraseTk = self.tokenizer.tokenize(self.linkingPhrase)

        # Cached values
        self.taggedPhrase = None
        self.tokenizedPhrase = None

    def getPhrase(self):
        return (self.lhConcept + ' ' + self.linkingPhrase + ' ' + 
                self.rhConcept)

    def getTokenizedPhrase(self):
        if self.tokenizedPhrase == None:
            self.tokenizedPhrase = []
            self.tokenizedPhrase.extend(self.lhConceptTk)
            self.tokenizedPhrase.extend(self.linkingPhraseTk)
            self.tokenizedPhrase.extend(self.rhConceptTk)

        return self.tokenizedPhrase

    def tag(self, tagger):
        '''Uses tagger to tag the phrase. Presents a single phrase to the tagger
           but, after the tagger returns, splits the tags among the tree parts
           of the sentence.
        '''
        
        self.taggedPhrase = tagger.tag(self.getTokenizedPhrase())

        linkingPhraseStart = len(self.lhConceptTk)
        rhConceptStart = linkingPhraseStart + len(self.linkingPhraseTk)
        
        self.taggedFirst = self.taggedPhrase[0:linkingPhraseStart]
        self.taggedLinkingPhrase = self.taggedPhrase[linkingPhraseStart:rhConceptStart]
        self.taggedSecond = self.taggedPhrase[rhConceptStart:]

        self.firstTaggedAsText = self.taggedAsText(self.taggedFirst)
        self.lpTaggedAsText = self.taggedAsText(self.taggedLinkingPhrase)
        self.secondTaggedAsText = self.taggedAsText(self.taggedSecond)

    def getTagged(self):
        return self.taggedPhrase

    @classmethod
    def taggedAsText(cls, taggedSequence):
        return ' '.join('%s/%s' % pair for pair in taggedSequence)


